﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Kompetition
{
    class Category
    {
        string name;
        Dictionary<Question, int> qPoint = new Dictionary<Question, int>();
        Dictionary<Question, bool> qAnswered = new Dictionary<Question, bool>();
        Question[] questions;

        public Category(string name, bool sort, Dictionary <Question, int> questions)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");
            if (questions == null)
                throw new ArgumentNullException("questions");
            this.name = name;

            if (!sort)
                this.qPoint = questions;

            foreach (var pair in questions)
                qAnswered.Add(pair.Key, false);

            List<Question> q = new List<Question>();
            foreach (var pair in questions)
                q.Add(pair.Key);
            this.questions = q.ToArray();
        }

        public bool AnswerQuestion(Question q, string answer)
        {
            if (q == null)
                throw new ArgumentNullException("q");
            if (answer == null)
                throw new ArgumentNullException("answer");
            if (string.IsNullOrEmpty(answer))
                return false;
            if (!qPoint.ContainsKey(q))
                throw new ArgumentException("q");

            bool toRet = q.IsCorrect(answer);
            if (toRet)
                qAnswered[q] = true;
            return toRet;
        }

        public int GetPointValue(Question q)
        {
            if (q == null)
                throw new ArgumentNullException("q");
            if (!qPoint.ContainsKey(q))
                throw new ArgumentException("q");
            return qPoint[q];
        }

        public bool IsQuestionAnswered(Question q)
        {
            if (q == null)
                throw new ArgumentNullException("q");
            if (!qPoint.ContainsKey(q))
                throw new ArgumentException("q");
            return qAnswered[q];
        }

        public string Name
        {
            get { return name; }
        }

        public Question[] Questions
        {
            get { return questions; }
        }
    }
}
